﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Management;
using System.Security.Cryptography;
using System.Text;

public class AppStatic
{
    public string publickkey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtFHvmu+Xa6QmYCTLYKaD5yhwXAkECxurB2X9JVxr9K00mArcUwlM74mx8pFscOxKBvMLVxp8hJZZxAiLrNXi3BFto5XNlUpWxKSrHKeXw+RLXjoeOvIhvSusMV2hKpsEhJzBP47PSLBz5cy1+ndhEHhFAXJZt13yQkXM9jqg3uKdD5Dhrx73RuUJI+oUgRtvdaDwIryMm8rX16csRQgmOrbV64cwC56JCwyGtGknDIKMetdeJuVxHX2mWURV4AS+Nl5698Ls0fzOoWGbDQRyz+hDk3TF9UwTcoAKdU/rzFkSaBxgJ/29eUAtKlLzxH20RDVWU3DD3IBv/4+enMYpJQIDAQAB";
    public static string ImageUri { get; set; }
    public static string BaseUrl { get; set; }

    public static string PersonUrl { get; set; }

    public static string Server { get; set; }
    public static string Port { get; set; }
    public static LocalTemp local { get; set; }
    public static bool isSave { get; set; }
    public static bool autoLogin { set; get; }
    public static bool isLoginOut { get; set; }

    public static bool needRegister { get; set; }

    public static List<MenuItemViewModel> Menu { get; set; }

    public static string token { get; set; }

    public static string deviceCode { get; set; }

    public static long userId { get; set; }

    public static RestUtils restUtils { get; set; }

    static AppStatic()
    {
        ImageUri = ConfigurationManager.AppSettings["imageUri"];
        BaseUrl = ConfigurationManager.AppSettings["baseUrl"];
        PersonUrl = ConfigurationManager.AppSettings["personUrl"];
        Server = ConfigurationManager.AppSettings["server"];
        Port = ConfigurationManager.AppSettings["port"];
        restUtils = new RestUtils();
    }

    public static bool init()
    {
        DataSet set = new DataSet();
        set.ReadXml(Environment.CurrentDirectory + "/" + "menu.xml");
        DataTable table = set.Tables[0];
        if (table.Rows.Count == 0)
        {
            return false;
        }
        Menu = new List<MenuItemViewModel>();
        for (int i = 0; i < table.Rows.Count; i++)
        {
            MenuItemViewModel menuItem = new MenuItemViewModel();
            menuItem.id = Convert.ToInt32(table.Rows[i]["id"].ToString());
            menuItem.name = table.Rows[i]["name"].ToString();
            menuItem.url = table.Rows[i]["url"].ToString().ToString();
            menuItem.pid = Convert.ToInt32(table.Rows[i]["pid"].ToString());
            menuItem.order = Convert.ToInt32(table.Rows[i]["order"].ToString());
            Menu.Add(menuItem);
        }

        DataSet configSet = new DataSet();
        configSet.ReadXml(Environment.CurrentDirectory + "/" + "config.xml");
        DataTable configTable = configSet.Tables[0];
        if (configTable.Rows.Count == 0)
        {
            return false;
        }
        local = new LocalTemp();
        local.account = configTable.Rows[0]["account"].ToString();
        local.passwd = configTable.Rows[0]["passwd"].ToString();
        local.isSave = configTable.Rows[0]["isSave"].ToString().Equals("1") ? true : false;
        local.autoLogin = configTable.Rows[0]["autoLogin"].ToString().Equals("1") ? true : false;
        return true;
    }

    public static void doAfterLogin(string account, string passwd)
    {
        DataSet configSet = new DataSet();
        configSet.ReadXml(Environment.CurrentDirectory + "/" + "config.xml");
        DataTable configTable = configSet.Tables[0];
        DataRow dr = null;
        bool isAdd = false;
        if (configTable.Rows.Count == 0)
        {
            dr = configTable.NewRow();
            isAdd = true;
        }
        else
        {
            dr = configTable.Rows[0];
        }
        dr["account"] = account;
        dr["passwd"] = passwd;
        dr["isSave"] = AppStatic.local.isSave ? "1" : "0";
        dr["autoLogin"] = AppStatic.local.autoLogin ? "1" : "0";
        if (isAdd)
        {
            configTable.Rows.Add(dr);
        }
        configTable.AcceptChanges();
        configSet.AcceptChanges();
        configSet.WriteXml(Environment.CurrentDirectory + "/" + "config.xml");

    }

    /// <summary>
    /// 读取设备信息 cpu 主板 bios
    /// </summary>
    /// <returns></returns>
    public static void readDeviceInfo() {
        ManagementClass mc = new ManagementClass("Win32_Processor");
        ManagementObjectCollection moc = mc.GetInstances();
        string cpuCode = "";
        foreach (ManagementObject mo in moc)
        {
            cpuCode = mo.Properties["ProcessorId"].Value.ToString();
            break;
        }

        //主板
        ManagementClass mc2 = new ManagementClass("Win32_BaseBoard");
        ManagementObjectCollection moc2 = mc2.GetInstances();
        string tableCode = "";
        foreach (ManagementObject mo in moc2)
        {
            tableCode += mo.Properties["SerialNumber"].Value.ToString();
            break;
        }

        //BIOS
        ManagementClass mc4 = new ManagementClass("Win32_BIOS");
        ManagementObjectCollection moc4 = mc.GetInstances();
        string biosCode = "";
        foreach (ManagementObject mo in moc4)
        {
            biosCode+= mo.Properties["SerialNumber"].Value.ToString();
            break;
        }
        if(string.IsNullOrEmpty(cpuCode) || string.IsNullOrEmpty(tableCode)||string.IsNullOrEmpty(biosCode))
        {
            throw new Exception("机器信息读取失败");
        }
        string info = string.Format("{0}-{1}-{2}", cpuCode, tableCode, biosCode);
        byte[] result = Encoding.Default.GetBytes(info);
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] output = md5.ComputeHash(result);
        deviceCode = BitConverter.ToString(output).Replace("-", ""); 
    }
}

public class MenuItemViewModel
{
    public string name { get; set; }
    public string url { get; set; }
    public int id { get; set; }
    public int pid { get; set; }
    public int order { get; set; }
}

public class LocalTemp
{
    public string account { get; set; }
    public string passwd { get; set; }

    public bool isSave { get; set; }
    public bool autoLogin { get; set; }
}